/*
 * @Author: kang_mou carl0608@126.com
 * @Date: 2023-04-21 10:07:23
 * @LastEditors: kang_mou kangh@gov-security.com
 * @LastEditTime: 2023-04-21 13:43:53
 * @FilePath: \create-sim\src\appName.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import chalk from "chalk";
import fs from "fs";
import prompts from "prompts";

const namePrompt = {
 type: "text",
 name: "name",
 message: "请输入项目名称",
 default: "sim-cli",
};

export const createDir = async (appName) => {
 while (1) {
  if (!appName) {
   const res = await prompts(namePrompt);
   appName = res.name;
  }
  try {
   const res = fs.mkdirSync(appName);
   break;
  } catch (err) {
   console.log(chalk.red("err：文件夹名称重复"));
   appName = null;
  }
 }
 return appName;
};

// 获取用户输入的名称
export const getCmdName = (option, program) => {
 if (option.name) return option.name;
 if (program.args && program.args.length > 0) return program.args[0];
 return null;
};
